/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.classdiagram.commands;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.requests.CreateRequest;
import org.eclipse.uml2.uml.TemplateParameter;
import org.eclipse.uml2.uml.TemplateSignature;

import com.cea.papyrus.core.commands.PapyrusCommand;
import com.cea.papyrus.core.helper.IGraphElementHelper;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * The Class Di2UMLElementCreateCommand.
 * This class is used to create the di2 element in the diagram with its UMLElement
 */
public class Di2TemplateParameterAddCommand extends PapyrusCommand{

	/**
	 * The location.
	 */
	private Point location=null;

	/**
	 * The size.
	 */
	private Dimension size=null;

	/**
	 * The uml parent.
	 */
	protected TemplateSignature umlTemplateSignature;

	/**
	 * The uml child.
	 */
	protected TemplateParameter referencedParameter;

	/**
	 * The parent GE.
	 */
	protected GraphElement parentGE;

	/**
	 * the created graphElement*.
	 */
	protected GraphElement childGe;

	/**
	 * request that creates this command.
	 */
	private CreateRequest createRequest;
	
	/**
	 * The Constructor.
	 * 
	 * @param element the element
	 * @param typeDiagram 
	 * @param parent the parent
	 */
	public Di2TemplateParameterAddCommand(java.lang.Class element, TemplateParameter referencedParameter, TemplateSignature parentSignature, GraphElement parent,String typeDiagram) {
		super(element);
		this.imodelerFactory = ModelerHelper.getFactory(typeDiagram);
		this.elementFactory = this.imodelerFactory.getElementFactory(this.elementClass);
		this.parentGE = parent;
		this.referencedParameter = referencedParameter ;
		this.umlTemplateSignature = parentSignature ;
		this.setLabel("\"creation of "+ element.getSimpleName()+"\"");
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public boolean canExecute() {
		return ((IGraphElementHelper)elementFactory).canCreateGraphElement(parentGE);
	}

	/**
	 * Execute.
	 */
	public void execute(){
		umlTemplateSignature.getParameters().add(referencedParameter) ;
		this.childGe=((IGraphElementHelper)elementFactory).createGraphElement(referencedParameter,parentGE,location,size);
	}

	/**
	 * Sets the location.
	 * 
	 * @param loc the loc
	 */
	public void setLocation (Point loc) {
		this.location = loc;
	}

	/**
	 * Redo.
	 */
	public void redo(){
		execute() ;
	}

	/**
	 * Undo.
	 */
	public void undo(){
		((IGraphElementHelper)elementFactory).removeGraphElement(childGe, parentGE);
		this.umlTemplateSignature.getParameters().remove(referencedParameter) ;
	}

	/**
	 * Sets the size.
	 * 
	 * @param dim the dim
	 */
	public void setSize(Dimension dim) {
		this.size=dim;
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	public CreateRequest getCreateRequest() {
		return createRequest;
	}

	/**
	 * 
	 * 
	 * @param createRequest 
	 */
	public void setCreateRequest(CreateRequest createRequest) {
		this.createRequest = createRequest;
	}
	

}
